package com.locatme.fragment;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.locatme.adapter.MessageViewAdapter;
import com.locatme.adapter.SenderViewAdapter;
import com.locatme.model.Message;
import com.locatme.model.Utilisateur;
import com.locatme.modelBD.MessageBD;
import com.main.locatme.R;

public class MessagesFragment extends Fragment {
    private ListView listView1;
    private ListView listView2;
    private MessageViewAdapter adapter;
    private SenderViewAdapter sendersAdapter;
    private List<Message> messages;
    private List<String> senders;
    private MessageBD messBD;
    private View rootView;

    // Utilisateur connect
    private Utilisateur user;


    public Utilisateur getUser() {
        return user;
    }

    public void setUser(Utilisateur user) {
        this.user = user;
    }

    public MessagesFragment() {
    }

    public MessageBD getMessBD() {
        return messBD;
    }

    public void setMessBD(MessageBD messBD) {
        this.messBD = messBD;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        messBD = new MessageBD(getActivity());
        messBD.open();
        messages = new ArrayList<Message>();
        senders = new ArrayList<String>();
        // Rcupration de l'objet dfinissant l'utilisateur connect
        user = (Utilisateur) getActivity().getIntent().getExtras().getParcelable("user");

        senders = messBD.RecupSenderByReceiver(user.getLogin());

        // Remplissage des donne de la premiere ListView
        sendersAdapter = new SenderViewAdapter(getActivity(),
                R.id.listviewsenders, senders);
        // On pose l'adapter sur la ListView

        if (senders != null) {
            rootView = inflater.inflate(R.layout.fragment_messages, container,
                    false);
            listView1 = (ListView) rootView.findViewById(R.id.listviewsenders);
            listView2 = (ListView) rootView.findViewById(R.id.listview);
            listView1.setAdapter(sendersAdapter);

            listView1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View v,
                                        int position, long id) {
                    TextView c = (TextView) v.findViewById(R.id.namefrom);
                    String sender = c.getText().toString();
                    messages = messBD.RecupMessagesByReceiver(sender,
                            user.getLogin());
                    if (messages != null) {
                        // ajout du nouvel adapter avec les nouveaux messages
                        adapter = new MessageViewAdapter(getActivity(),
                                R.id.listview, messages);
                        listView2.setAdapter(adapter);
                    } else {
                        Toast.makeText(getActivity(),
                                "Messages problem, try later",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });

        } else {
            rootView = inflater.inflate(R.layout.fragment_nomessage, container,
                    false);
        }
        return rootView;
    }

}
